using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Ext.Direct.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using TestApplication.Models;
using System.Linq.Dynamic;

namespace TestApplication.Controllers
{
    public class EmployeesController : DirectController
    {
        readonly SampleDataContext _db = new SampleDataContext();

        public ActionResult Get(int start, int limit, string sort, string dir) {
            Type requestedTableType = System.Type.GetType("TestApplication.Models.Employee");
            System.Data.Linq.ITable table = _db.GetTable(requestedTableType);
            var employeeList = table //_db.Employees
                .OrderBy(sort + " " + dir)
                .Skip(start)
                .Take(limit);

            var result = new {
                total = _db.Employees.Count(),
                data = employeeList
            };

            return Direct(result, new List<JsonConverter> {new IsoDateTimeConverter()});
        }
    }
}
